/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.seata.sqlparser.druid.oscar;

import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleArgumentExpr;
import org.apache.seata.sqlparser.ParametersHolder;
import org.apache.seata.sqlparser.SQLType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * The type Oscar delete recognizer test.
 */
public class OscarDeleteRecognizerTest extends AbstractOscarRecognizerTest {

    @Test
    public void testGetSqlType() {
        String sql = "delete from t where id = ?";
        SQLStatement sqlStatement = getSQLStatement(sql);

        OscarDeleteRecognizer recognizer = new OscarDeleteRecognizer(sql, sqlStatement);
        Assertions.assertEquals(recognizer.getSQLType(), SQLType.DELETE);
    }

    @Test
    public void testGetTableAlias() {
        String sql = "delete from t where id = ?";
        SQLStatement sqlStatement = getSQLStatement(sql);

        OscarDeleteRecognizer recognizer = new OscarDeleteRecognizer(sql, sqlStatement);
        Assertions.assertNull(recognizer.getTableAlias());
    }

    @Test
    public void testGetTableName() {
        String sql = "delete from t where id = ?";
        SQLStatement sqlStatement = getSQLStatement(sql);

        OscarDeleteRecognizer recognizer = new OscarDeleteRecognizer(sql, sqlStatement);
        Assertions.assertEquals(recognizer.getTableName(), "t");
    }

    @Test
    public void testGetWhereCondition_0() {
        String sql = "delete from t";
        SQLStatement sqlStatement = getSQLStatement(sql);

        OscarDeleteRecognizer recognizer = new OscarDeleteRecognizer(sql, sqlStatement);
        String whereCondition = recognizer.getWhereCondition(() -> null, new ArrayList<>());

        // test for no condition
        Assertions.assertEquals("", whereCondition);

        sql = "delete from t where id = ?";
        sqlStatement = getSQLStatement(sql);

        recognizer = new OscarDeleteRecognizer(sql, sqlStatement);
        whereCondition = recognizer.getWhereCondition(
                new ParametersHolder() {
                    @Override
                    public Map<Integer, ArrayList<Object>> getParameters() {
                        ArrayList<Object> idParam = new ArrayList<>();
                        idParam.add(1);
                        Map result = new HashMap();
                        result.put(1, idParam);
                        return result;
                    }
                },
                new ArrayList<>());

        // test for normal sql
        Assertions.assertEquals("id = ?", whereCondition);

        sql = "delete from t where id in (?)";
        sqlStatement = getSQLStatement(sql);
        recognizer = new OscarDeleteRecognizer(sql, sqlStatement);
        whereCondition = recognizer.getWhereCondition(
                new ParametersHolder() {
                    @Override
                    public Map<Integer, ArrayList<Object>> getParameters() {
                        ArrayList<Object> idParam = new ArrayList<>();
                        idParam.add(1);
                        Map result = new HashMap();
                        result.put(1, idParam);
                        return result;
                    }
                },
                new ArrayList<>());

        // test for sql with in
        Assertions.assertEquals("id IN (?)", whereCondition);

        sql = "delete from t where id between ? and ?";
        sqlStatement = getSQLStatement(sql);
        recognizer = new OscarDeleteRecognizer(sql, sqlStatement);
        whereCondition = recognizer.getWhereCondition(
                new ParametersHolder() {
                    @Override
                    public Map<Integer, ArrayList<Object>> getParameters() {
                        ArrayList<Object> idParam = new ArrayList<>();
                        idParam.add(1);
                        ArrayList<Object> idParam2 = new ArrayList<>();
                        idParam.add(2);
                        Map result = new HashMap();
                        result.put(1, idParam);
                        result.put(2, idParam2);
                        return result;
                    }
                },
                new ArrayList<>());
        // test for sql with in
        Assertions.assertEquals("id BETWEEN ? AND ?", whereCondition);

        // test for exception
        Assertions.assertThrows(IllegalArgumentException.class, () -> {
            String s = "delete from t where id in (?)";
            SQLDeleteStatement deleteAst = (SQLDeleteStatement) getSQLStatement(s);
            deleteAst.setWhere(new OracleArgumentExpr());
            new OscarDeleteRecognizer(s, deleteAst).getWhereCondition(() -> new HashMap<>(), new ArrayList<>());
        });
    }

    @Test
    public void testGetWhereCondition_1() {

        String sql = "delete from t";
        SQLStatement sqlStatement = getSQLStatement(sql);

        OscarDeleteRecognizer recognizer = new OscarDeleteRecognizer(sql, sqlStatement);
        String whereCondition = recognizer.getWhereCondition();

        // test for no condition
        Assertions.assertEquals("", whereCondition);

        sql = "delete from t where id = 1";
        sqlStatement = getSQLStatement(sql);

        recognizer = new OscarDeleteRecognizer(sql, sqlStatement);
        whereCondition = recognizer.getWhereCondition();

        // test for normal sql
        Assertions.assertEquals("id = 1", whereCondition);

        sql = "delete from t where id in (1)";
        sqlStatement = getSQLStatement(sql);
        recognizer = new OscarDeleteRecognizer(sql, sqlStatement);
        whereCondition = recognizer.getWhereCondition();

        // test for sql with in
        Assertions.assertEquals("id IN (1)", whereCondition);

        sql = "delete from t where id between 1 and 2";
        sqlStatement = getSQLStatement(sql);
        recognizer = new OscarDeleteRecognizer(sql, sqlStatement);
        whereCondition = recognizer.getWhereCondition();
        // test for sql with in
        Assertions.assertEquals("id BETWEEN 1 AND 2", whereCondition);

        // test for exception
        Assertions.assertThrows(IllegalArgumentException.class, () -> {
            String s = "delete from t where id in (1)";
            SQLDeleteStatement deleteAst = (SQLDeleteStatement) getSQLStatement(s);
            deleteAst.setWhere(new OracleArgumentExpr());
            new OscarDeleteRecognizer(s, deleteAst).getWhereCondition();
        });
    }
}
